package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private JedisPool jedisPool;


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();

        PageHelper.startPage(currentPage, pageSize);

        Page<Setmeal> checkGroups = setmealDao.findPage(queryString);


        return new PageResult(checkGroups.getTotal(), checkGroups.getResult());
    }

    @Override
    public void add(Integer[] checkgroupids, Setmeal setmeal) {
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
        setmealDao.add(setmeal);
        System.out.println(setmeal.getId());
        setT_setmeal_checkgroup(checkgroupids, setmeal.getId());
    }

    @Override
    public Result del(Integer id) {
        Integer count = setmealDao.findSetmealIds(id);

        if (count != 0) {
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL_middle);
        } else {

            Setmeal setmeal = setmealDao.findById(id);
            //删掉redis数据删掉七牛云图片
            jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
            QiniuUtils.deleteFileFromQiniu(setmeal.getImg());
            setmealDao.delCheckgroupIdsById(id);
            setmealDao.del(id);
            return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
        }
    }

    @Override
    public List<Setmeal> getAllSetmeal() {
        return setmealDao.getAllSetmeal();
    }

    @Override
    public Setmeal findById(int id) {
        Setmeal setmeal = setmealDao.findById(id);
        List<Integer> cgIds = setmealDao.findCheckGroupIdsById(id);
        List<CheckGroup> list = new ArrayList<>();
        for (Integer cgId : cgIds) {
            CheckGroup checkGroup = checkGroupDao.findById(cgId);
            List<Integer> checkItemIds = checkGroupDao.findCheckItemIds(cgId);
            List<CheckItem> checkItemList = new ArrayList<>();
            for (Integer checkItemId : checkItemIds) {
                CheckItem checkItem = checkItemDao.findById(checkItemId);
                checkItemList.add(checkItem);
            }
            checkGroup.setCheckItems(checkItemList);
            list.add(checkGroup);
        }
        setmeal.setCheckGroups(list);
        return setmeal;
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setmealDao.findSetmealCount();


    }

    public void setT_setmeal_checkgroup(Integer[] checkgroupids, Integer setmealId) {
        Map<String, Integer> map = new HashMap<>();
        if (checkgroupids != null && checkgroupids.length > 0) {
            for (Integer checkgroupId : checkgroupids) {
                map.put("checkgroupId", checkgroupId);
                map.put("setmealId", setmealId);
                setmealDao.addMiddleTable(map);
            }
        }
    }
}
